package own.stu.jobgib.playown.netty.ch6;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;

/**
 * @author
 */
@Slf4j
public class OutBoundHandlerC extends ChannelOutboundHandlerAdapter {

    public String name() {
        return "OutBoundHandlerC";
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        System.out.println("OutBoundHandlerC[flush]: ");
        super.flush(ctx);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("OutBoundHandlerC[write]: " + msg);
        ctx.write(msg, promise);
//        throw new BusinessException("from OutBoundHandlerC");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        log.info("OutBoundHandlerC[handlerAdded]");

        ctx.channel().writeAndFlush("hello world - 1");
//        ctx.channel().write("hello world - 1");
        ctx.write("hello world -- 2");
//        ctx.pipeline().write("hello world - 3");
//        ctx.channel().pipeline().write("hello world - 4");
//        ctx.channel().write("hello world");

        /*ctx.executor().schedule(() -> {
            ctx.channel().write("hello world");
            ctx.write("hello world");
        }, 3, TimeUnit.SECONDS);*/
    }
}
